<?php

namespace app\apiadmin\controller\liveManager;

use app\apiadmin\service\liveManager\LiveOnlineService;
use app\apiadmin\service\statics\LiveRecoderService;
use app\apiadmin\service\TCSS\TImService;
use app\common\controller\BaseController;
use app\common\utils\Res;
use think\facade\Db;
use think\facade\Log;

/**
 *  LiveOnline 直播在线
 *  1 在线人数 离线人数
 *  2 销售列表
 *  3 销售邀请在线列表
 */
class LiveOnline extends BaseController
{
    //获取所有销售列表
    public function getSalerList()
    {
        $param = $this->request->post();
        $result = LiveOnlineService::salerList($param);
        return $result;
    }
    //获取在线、离线用户
    public function getLineUser(){
        $param = $this->request->post();
        $result = LiveOnlineService::getLineUser($param);
        return $result;
    }
    //获取评论列表
    public function getPinglunList(){
        $param = $this->request->post();
        $result = LiveOnlineService::getPinglunList($param);
        return $result;
    }
    public function isUserSaler(){
        $param = $this->request->post();
        $userName = $this->request->userName;
        $result = LiveOnlineService::isUserSaler($param,$userName);
        return $result;
    }
    //获取用户邀请用户id列表
    public function getSalerInvatationUserIds(){
        $param = $this->request->post();
        $userName = $this->request->userName;
        $result = LiveOnlineService::getSalerInvatationUserIds($param,$userName);
        return $result;
    }
    public function getPlayInfo(){
        $param = $this->request->post();
        $result = LiveOnlineService::getPlayInfo($param);
        return $result;
    }
    //设置商品销售状态 需要在直播界面显示或者不显示弹框
    public function sendCourseSellCommand(){

        $param = $this->request->post();
        if (empty($param["chartId"])) {
            return Res::error("chartId不能为空");
        }

        if (empty($param["command"])) {
            return Res::error("command不能为空");
        }
        $tim = new TImService("administrator");
        $ret = $tim->message_system_notify_send_to_group($param["chartId"], $param["command"]);

        $liveReocde = new LiveRecoderService();
        $liveReocde->RecodeCourseSellMessage($param["chartId"], $param["command"],'course_sell_message');

        if ($ret === true) {
            return Res::ok("发送成功");
        } else {
            return Res::error($ret);
        }
        
    }
    //更改直播状态
    public function sendCourseSellCmd()
    {
        // liveid cmd params
        $param = $this->request->post();
        $uid = $this->request->uid;
        if (empty($param["liveId"]) || empty($param["command"])) {
            return Res::error("参数不正确,请检查参数!!");
        }
        $liveId = $param["liveId"];
        $cmd = $param["command"];
        $status = $param['status']; //1为售罄 0为取消售罄
        $params = $param["params"];
        $liveReocde = new LiveRecoderService();

        try {
            $liveStatus = new LiveStatus($liveId, $uid);
            $goodsId = $params['goodsId'];
            if($status==0){
                $redis_goodsIds = $liveStatus->getSellOutGoodIdList($liveId);
                if($redis_goodsIds){
                    $redis_goodsIds = explode(',',$redis_goodsIds);
                    $rs = array_filter($redis_goodsIds, function($item) use ($goodsId){
                        return $item != $goodsId;
                    });
                    
                    $liveStatus->setSellOutGoodIdList($liveId,implode(',',$rs));
                }
            }
            if($status==1){
                $redis_goodsIds = $liveStatus->getSellOutGoodIdList($liveId);
                if($redis_goodsIds){
                    $redis_goodsIds.=','.$goodsId;
                }
                $liveStatus->setSellOutGoodIdList($liveId,$goodsId);
            }
            $ret = $liveStatus->ProcessCmd($cmd, $params);

            // 执行完毕如mongodb 执行记录
            $liveReocde->RecodeCommand($liveId, $cmd, $params);
            
            $data = $liveStatus->getSellOutGoodIdList($liveId);
            return Res::ok([$data]);
            //return Res::ok($ret);
            
        } catch (\Throwable $th) {
            return Res::error($th->getMessage());
        }
    }
    
    public function getSellOutGoodsIdList(){
        
        $param = $this->request->post();
        if (empty($param["liveId"])) {
            return Res::error("参数不正确,请检查参数!!");
        }
        $liveId = $param["liveId"];
        $liveStatus = new LiveStatus($liveId, $uid=0);
        $data = $liveStatus->getSellOutGoodIdList($liveId);
        return Res::ok([$data]);
        
    }
    
    
}
